/*
 * 	Flan.Zeng 2011-2016	http://git.oschina.net/signup?inviter=flan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flan.stock.fragment;

import java.util.ArrayList;
import java.util.List;

import com.android.volley.Request;
import com.android.volley.Request.Method;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.Response.ErrorListener;
import com.flan.stock.StockApplication;
import com.flan.stock.activity.NewsDetailActivity;
import com.flan.stock.R;
import com.flan.stock.adapter.NewsYwListViewAdapter;
import com.flan.stock.bean.News;
import com.flan.stock.net.ObjectRequest;
import com.flan.stock.utils.DensityUtil;
import com.flan.stock.view.TouTiaoPagerView;
import com.google.gson.reflect.TypeToken;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

/**
 * 显示资讯窗口中 要闻信息的 Fragment
 * @author flan
 * @date   2015年11月6日
 */
public class FragmentNewsYw extends Fragment{
	
	private Context context;
	private TouTiaoPagerView pagerView;
	private ListView lv_yaowen;
	
	private List<News> newsList;
	private List<View> toutiaoList;
	
	private NewsYwListViewAdapter ywAdapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.context = getContext();
		getTouTiaoData();
		getNewsList();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		lv_yaowen = new ListView(context);
		lv_yaowen.setLayoutParams(params);
		
		pagerView = new TouTiaoPagerView(context, getResources().getLayout(R.layout.layout_news_toutio));
		params.height = DensityUtil.dip2px(context, 150);
		pagerView.setLayoutParams(params);
		pagerView.setPagerList(toutiaoList);
		pagerView.setOnPageChangeListener(pagerView);
		lv_yaowen.addHeaderView(pagerView);
		
		ywAdapter = new NewsYwListViewAdapter(context, newsList);
		lv_yaowen.setAdapter(ywAdapter);
		
		lv_yaowen.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
				//Toast.makeText(context, "position "+position+" ; id "+id, Toast.LENGTH_SHORT).show();;
				Intent intent = new Intent(context, NewsDetailActivity.class);
				intent.putExtra("news", newsList.get(position-1));
				context.startActivity(intent);
			}
		});
		
		return lv_yaowen;
	}
	
	@Override
	public void onStart() {
		super.onStart();
		
	}
	
	/**
	 * 获取新闻列表数据
	 */
	private void getNewsList(){
		
		//String url = "http://112.124.121.218:8080/edu/stock/newslist.html";
		String url = context.getResources().getString(R.string.url_news_yaowen_lv);
		
		Listener<List<News>> successListener = new Listener<List<News>>() {

			@Override
			public void onResponse(List<News> resultList) {
				newsList = resultList;
				ywAdapter.setDate(resultList);
				//System.out.println("============== "+resultList.get(0).getTitle());
			}
		};
		
		ErrorListener errorListener = new ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError arg0) {
				System.out.println("============== 失败");
			}
		};
		
		Request<List<News>> newsRequest = new ObjectRequest<>(Method.GET, new TypeToken<List<News>>(){}.getType(), url, successListener,errorListener);
		StockApplication.requestQueue.add(newsRequest);
	}
	
	/**
	 * 获取头条新闻数据
	 */
	private void getTouTiaoData(){
		
		LayoutParams para = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		
		ImageView img1 = new ImageView(context);
		img1.setBackgroundResource(R.drawable.img_ipo);
		img1.setLayoutParams(para);
		ImageView img2 = new ImageView(context);
		img2.setBackgroundColor(Color.BLUE);
		img2.setLayoutParams(para);
		toutiaoList = new ArrayList<>();
		toutiaoList.add(img1);
		toutiaoList.add(img2);
	}
}
















